package com.scalar.chiptrack.sales.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.ExcelUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.sales.reports.dao.DataDumpDao;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jul 4, 2007
 * Time: 3:05:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataDumpReportAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/dataDumpReportAction.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {

        String action = request.getParameter( "action" );
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        Workbook workbook = null;
        String fileName = "";

        if ( action == null )
        {
            action = "";
        }
        try
        {
            if ( "generateScrapLotsXlsReport".equalsIgnoreCase(action) )
            {
                ArrayList scrapList = null;

                String psType       = request.getParameter( "prompt0" );
                String productId    = request.getParameter( "prompt1" );
                String vendor       = request.getParameter( "prompt2" );
                String lotcategory  = request.getParameter( "prompt3" );
                String fromDate     = request.getParameter( "prompt4" );
                String toDate       = request.getParameter( "prompt5" );
                String reportType   = request.getParameter( "prompt6" );
                String scrapType   =  request.getParameter( "prompt7" );
                String defaultPrompt = request.getParameter( "prompt8" );
                int scrapTypeInt=2;
                try
                {
                  scrapTypeInt = Integer.parseInt(scrapType);
                }
                catch(NumberFormatException e)
                {
                    scrapTypeInt = 2;
                }
                scrapList = DataDumpDao.loadScrapLotsByProcessType( null,psType,productId,vendor,lotcategory,fromDate,toDate,reportType, scrapTypeInt, defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( scrapList, "Scrap Lots Report", fromDate, toDate, null );
                fileName = "ScrapLotsReport_" + DateUtil.getTimeStamp() ;
            }
            if ( "generateShipmentReportbyLotNumberXls".equalsIgnoreCase(action) )
            {
                int orderTypeInt = -1;
                ArrayList vendorShipmentList = null;

                String customerIds  = request.getParameter( "prompt0" );
                String productId    = request.getParameter( "prompt1" );
                String soNubmer     = request.getParameter( "prompt2" );
                String lotNubmer    = request.getParameter( "prompt3" );
                String category     = request.getParameter( "prompt4" );
                String fromDate     = request.getParameter( "prompt5" );
                String toDate       = request.getParameter( "prompt6" );
                String reportType   = request.getParameter( "prompt7" );
                String defaultPrompt = request.getParameter( "prompt8" );

                orderTypeInt = Integer.parseInt(category);

                vendorShipmentList = DataDumpDao.loadShipmentReprotbyLotNumber( null,customerIds,productId,soNubmer,lotNubmer,orderTypeInt,fromDate,toDate,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( vendorShipmentList, "Shipment Report by Lot Number", fromDate, toDate, null );
                fileName = "ShipmentReportbyLotNumber_" + DateUtil.getTimeStamp() ;
            }
            if ( "generateBoardAndKitShipmentReportXls".equalsIgnoreCase(action) )
            {
                int orderTypeInt = -1;
                ArrayList vendorShipmentList = null;

                String customerIds  = request.getParameter( "prompt0" );
                String productId    = request.getParameter( "prompt1" );
                String soNubmer     = request.getParameter( "prompt2" );
                String lotNubmer    = request.getParameter( "prompt3" );
                String category     = request.getParameter( "prompt4" );
                String fromDate     = request.getParameter( "prompt5" );
                String toDate       = request.getParameter( "prompt6" );
                String reportType   = request.getParameter( "prompt7" );
                String defaultPrompt = request.getParameter( "prompt8" );

                orderTypeInt = Integer.parseInt(category);

                vendorShipmentList = DataDumpDao.loadBoardandKitShipmentReport( null,customerIds,productId,soNubmer,lotNubmer,orderTypeInt,fromDate,toDate,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( vendorShipmentList, "Board and Kit Shipment Report", fromDate, toDate, null);
                fileName = "BoardAndKitShipmentReport_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateReleaseReportXls".equalsIgnoreCase(action) )
            {
                ArrayList releaseList = null;

                String productIds  = request.getParameter( "prompt0" );
                String vendorIds    = request.getParameter( "prompt1" );
                String status     = request.getParameter( "prompt2" );
                String category    = request.getParameter( "prompt3" );
                String fromDate     = request.getParameter( "prompt4" );
                String toDate     = request.getParameter( "prompt5" );
                String isDetail       = request.getParameter( "prompt6" );
                String reportType   = request.getParameter( "prompt7" );
                String defaultPrompt = request.getParameter( "prompt8" );
                String psType = request.getParameter( "prompt9" );

                releaseList = DataDumpDao.loadReleaseReportbyPsType( null,productIds,vendorIds,status,category,fromDate,toDate,isDetail,reportType,defaultPrompt, psType);
                
                if (Constants.LOCATION_TYPE_FT.equalsIgnoreCase( psType ) )
                {
                    psType = Constants.LOCATION_TYPE_TEST;
                }
                workbook =  DataDumpReportsHelper.getWorkbook( releaseList,  psType + " Release Report", fromDate, toDate, null );
                fileName = psType + " ReleaseReport_" + DateUtil.getTimeStamp() ;
            }
            else if ( "binningDiscrepanyReportXls".equalsIgnoreCase(action) )
            {
                //ArrayList releaseList = null;

                String productIds  = request.getParameter( "prompt0" );
                String vendorIds    = request.getParameter( "prompt1" );
                Map<String,String> paramMap = new HashMap<String,String>();
                paramMap.put ( "processStepId", productIds );
                paramMap.put ( "vendorId", vendorIds );

                //releaseList = DataDumpDao.getBinningDiscrepanyReport( null,productIds,vendorIds );
                workbook = DataDumpDao.getBinningDiscrepanyReportWorkbook( null, paramMap);
                //workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "Binning Discrepacy Report",null,null,null );
                fileName = "Binning Discrepacy Report_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateBinningReportsXls".equalsIgnoreCase(action) )
            {
                ArrayList releaseList = null;

                String psType       = request.getParameter( "prompt0" );
                String productIds   = request.getParameter( "prompt1" );
                String vendorIds    = request.getParameter( "prompt2" );
                String category     = request.getParameter( "prompt3" );
                String searchPartNo     = request.getParameter( "prompt4" );
                String isDetail     = request.getParameter( "prompt5" );
                String fromDate     = request.getParameter( "prompt6" );
                String toDate       = request.getParameter( "prompt7" );
                String reportType   = request.getParameter( "prompt8" );
                String defaultPrompt = request.getParameter( "prompt9" );

                releaseList = DataDumpDao.loadBinningInfobyProcess( null,psType,productIds,vendorIds,category,searchPartNo,isDetail,fromDate,toDate,reportType,defaultPrompt);

                if ("Sort".equalsIgnoreCase(psType) && "Detail".equalsIgnoreCase(isDetail))
                {
                    workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "Sort Detail Binning Info Report",fromDate,toDate,null );
                    fileName = "Sort Detail Binning Info Report_" + DateUtil.getTimeStamp() ;
                }
                else if ("Sort".equalsIgnoreCase(psType) && "Summary".equalsIgnoreCase(isDetail))
                {
                    workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "Sort Summary Binning Info Report",null,null,null );
                    fileName = "Sort Summary Binning Info Report_" + DateUtil.getTimeStamp() ;
                }
                else
                {
                    workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "Test Binning Info Report",null,null,null );
                    fileName = "Test Binning Info Report_" + DateUtil.getTimeStamp() ;
                }
            }
            else if ( "generateBoardKitInventoryReportXls".equalsIgnoreCase(action) )
            {
                ArrayList releaseList = null;

                String productType  = request.getParameter( "prompt0" );
                String productIds    = request.getParameter( "prompt1" );
                String vendorIds     = request.getParameter( "prompt2" );
                String asofdate    = request.getParameter( "prompt3" );
                String category     = request.getParameter( "prompt4" );
                String status     = request.getParameter( "prompt5" );
                String reportType   = request.getParameter( "prompt6" );
                String defaultPrompt = request.getParameter( "prompt7" );

                releaseList = DataDumpDao.loadBoardKitInventoryReport( null,productType,productIds,vendorIds,asofdate,category,status,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "Board Kit Inventory Report",null,null,null );
                fileName = "BoardKitInventoryReport_"+DateUtil.getTimeStamp();
            }
            else if ( "generateXLS_CRD_Sch_Rev_Change".equalsIgnoreCase(action) )
            {
                ArrayList releaseList = null;

                String customerIds  = request.getParameter( "prompt0" );
                String productIds    = request.getParameter( "prompt1" );
                String soNumber     = request.getParameter( "prompt2" );
                String status    = request.getParameter( "prompt3" );
                String category     = request.getParameter( "prompt4" );
                String fromDate     = request.getParameter( "prompt5" );
                String toDate   = request.getParameter( "prompt6" );
                String reportType = request.getParameter( "prompt7" );
                String defaultPrompt = request.getParameter( "prompt8" );

                releaseList = DataDumpDao.loadCRDSchDateRevisionChangeLogReport( null,customerIds,productIds,soNumber,status,category,fromDate,toDate,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( releaseList, "CRD Sch Change Revision Report",fromDate,toDate,null );
                fileName = "CRD_Sch_Change_Report_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateProductionReportXls".equalsIgnoreCase(action) )
            {
                ArrayList productList = null;

                String psType  = request.getParameter( "prompt0" );
                String vendorId    = request.getParameter( "prompt1" );
                String fromDate     = request.getParameter( "prompt2" );
                String toDate   = request.getParameter( "prompt3" );
                String defaultPrompt = request.getParameter( "prompt4" );

                productList = DataDumpDao.loadProductionReportDetails( null,psType,vendorId,fromDate,toDate,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( productList, "Production Report",fromDate,toDate,null );
                fileName = "Production_Report_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateSalesInfoReportXls".equalsIgnoreCase(action) )
            {
                ArrayList salesInfoList = null;
                int orderTypeInt = -1;

                String customerId  = request.getParameter( "prompt0" );
                String productId    = request.getParameter( "prompt1" );
                String endUser     = request.getParameter( "prompt2" );
                String endCustomer   = request.getParameter( "prompt3" );
                String soNumner = request.getParameter( "prompt4" );
                String salesPerson = request.getParameter( "prompt5" );
                String status = request.getParameter( "prompt6" );
                String category = request.getParameter( "prompt7" );
                String orderType = request.getParameter( "prompt8" );
                String fromDate = request.getParameter( "prompt9" );
                String toDate = request.getParameter( "prompt10" );
                String reportType = request.getParameter( "prompt11" );
                String defaultPrompt = request.getParameter( "prompt12" );

                orderTypeInt = Integer.parseInt(orderType);
                salesInfoList = DataDumpDao.loadSalesOrderInfoReport( null,customerId,productId,endUser,endCustomer,soNumner,salesPerson,status,category,orderTypeInt,fromDate,toDate,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( salesInfoList, "Sales Orders Info Report", fromDate,toDate,null );
                fileName = "Sales_Orders_Info_Report_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateAccruedLiabilityXlsReport".equalsIgnoreCase(action) )
            {
                ArrayList liabilityList = null;

                String psType           = request.getParameter( "prompt0" );
                String productIds       = request.getParameter( "prompt1" );
                String vendorId         = request.getParameter( "prompt2" );
                String category         = request.getParameter( "prompt3" );
                String fromDate         = request.getParameter( "prompt4" );
                String toDate           = request.getParameter( "prompt5" );
                String psTypeName       = request.getParameter( "prompt6" );
                String reportType       = request.getParameter( "prompt7" );
                String defaultPrompt    = request.getParameter( "prompt8" );

                liabilityList = DataDumpDao.loadAccruedLiabilityReport( null,psType,productIds,vendorId,category,fromDate,toDate,psTypeName,reportType,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( liabilityList, "Accrued Liability Report",fromDate,toDate,null );
                fileName = "Accrued_Liability_Report_" + DateUtil.getTimeStamp() ;
            }
            else if ( "generateAssemblyInstructionXlsReport".equalsIgnoreCase(action) )
            {
                ArrayList assemblyList = null;

                String productId        = request.getParameter( "prompt0" );
                String processstepIds   = request.getParameter( "prompt1" );
                String dateType         = request.getParameter( "prompt2" );
                String fromDate         = request.getParameter( "prompt3" );
                String toDate           = request.getParameter( "prompt4" );
                String defaultPrompt    = request.getParameter( "prompt5" );

                assemblyList = DataDumpDao.loadAssemblyInstructionReport( null,productId,processstepIds,dateType,fromDate,toDate,defaultPrompt);

                workbook =  DataDumpReportsHelper.getWorkbook( assemblyList, "Assembly Instruction Report",fromDate,toDate,null );
                fileName = "Assembly_Instruction_Report_" + DateUtil.getTimeStamp() ;
            }

            ExcelUtils.generateExcelReport( workbook, fileName, response );
        }
        catch ( Exception e)
        {
            //errors.put ( "Could not generate excel file", null );
            e.printStackTrace();
        }

       return null;
    }

}
